#include "head.h"
int do_worker(int net_fd, MYSQL* mysql){
    printf("workering!\n");
    while(1){
        char isEnter = '\0';
        // int ret_recv;

        int ret_recv=recv(net_fd, &isEnter, sizeof(char), MSG_WAITALL);
        if(ret_recv==0){
            return 0;
        }
        printf("%c\n", isEnter);
        if(isEnter == '?'){
            //接收命令
            printf("work\n");
            char cmd;
            recv(net_fd, &cmd, sizeof(char), MSG_WAITALL);
            switch(cmd){
            case 'l':case 'L':{
                                  sign_in(net_fd, mysql);
                                  break;
                              }
            case 'r':case 'R':{
                                  sign_up(net_fd, mysql);
                                  break;
                              }
            default:{
                        break;
                    }
            }
            continue;
        }
        /* if(strcmp(cmd,"up") == 0){ */
        /*     sign_up(net_fd); */
        /*     continue; */
        /* }else if(strcmp(cmd,"in") == 0){ */
        /*     //sign_in(net_fd); */
        /* } */
        command_t* command=(command_t*)calloc(1,sizeof(command_t));
        /* char cut_buf[1000]={0}; */

        //接收buf
        off_t buf_len = 0;
        ret_recv=recv(net_fd, &buf_len, sizeof(off_t), MSG_WAITALL);
        if(ret_recv==0){
            return 0;
        }
        ret_recv=recv(net_fd, command->buf, buf_len, MSG_WAITALL); 
        if(ret_recv==0){
            return 0;
        }
        /* printf("command->buf = %s\n",command->buf); */

        char* user_id = strtok(command->buf, "&");
        /* printf("user_id: %s\n", user_id); */

        char* comd = strtok(NULL, "&");
        /* printf("comd=%s\n",comd); */

        command->path = strtok(NULL, "&");
        /* printf("path=%s\n",command->path); */
        char* cmds=strtok(comd," \n");
        /* printf("cmds: %s\n", cmds); */
        char* arg[10];
        int arg_count=0;
        while ((arg[arg_count] = strtok(NULL, " \n")) != NULL) {
            arg_count++;
        }
        //命令参数的个数
        command->token_num=arg_count;
        //命令  参数&路径
        /* printf("%d: %s\n", arg_count, arg[0]); */
        /* sleep(10); */
        if (strcmp(cmds, "exit") == 0) {
            printf("客户端请求退出，关闭连接\n");
            break; // 结束当前客户端的处理，返回以关闭连接
        }
        if(strcmp(cmds,"cd")==0){
            //todo,执行cd函数
            changedir(command->path,arg[0], net_fd,mysql,user_id);
        }else if(strcmp(cmds,"ls")==0){
            //todo
            command->types=LS;
            lsCommand(command,net_fd,mysql,user_id);
        }else if(strcmp(cmds,"puts")==0){
            //todo
            snd_cmd(net_fd,command,mysql);
        }else if(strcmp(cmds,"gets")==0){
            //todo
        }else if(strcmp(cmds,"rm")==0){
            //todo
            removefile(net_fd, command->path, arg[1], user_id, mysql);
        }else if(strcmp(cmds,"pwd")==0){
            //todo
            command->types=PWD;
            pwdCommand(command);
        }
        else if(strcmp(cmds,"mkdir") == 0){
            // TODO : mkdir();
            mkdirs(mysql,arg[0],command->path,net_fd,atoi(user_id));
        }
        else{
            char error_msg[100]={0};
            recv(net_fd,error_msg,sizeof(error_msg),0);
            LOG_MSG(INFO,error_msg);
            //todo，打印到日志
        }

    }    
    return 0;
}
